<?php

namespace Wechat\Controller;

use Common\Model\UserFavoritesModel;

class QuestionController extends CommonController
{
    public function lists()
    {
        $this->assign('pagemark', 'question');
        $this->display();
    }

    public function view()
    {
        $uid = session('ytata.uid');
        $user = D('User')->get_user_one($uid, true, 1);
        $this->assign('uid', $uid);
        $this->assign('user', $user);
        $this->assign('pagemark', 'question');
        $this->display();
    }

    // 问题列表
    public function ajax_question_list()
    {

        $p = I('get.p', '1', 'intval');
        $startrow = ($p - 1) * 5;
        $this->assign(compact('startrow'));
        $html = $this->fetch('ajax_question_list');
        if (substr($html, -13) == '<!--empty -->') {
            $this->ajaxReturn(0, '没有数据');
        }
        $this->ajaxReturn(1, '', array(
            'html'    => $html,
            'loadurl' => query_url('ajax_question_list', array('p' => $p + 1), '', true),
        ));
    }

    // 提交问题
    public function ajax_question_add()
    {
        $html = $this->fetch();
        $this->ajaxReturn(1, '', $html);
    }

    // 提交问题保存
    public function ajax_question_save()
    {
        $this->check_login();
        $data = I('post.');

        if (!isset($data['describe']) || empty($data['describe'])) {
            $this->ajaxReturn(0, '请填写问题描述');
        }

        $uid = session('ytata.uid');
        $question = D('Question');
        $ret = $question->question_add($uid, $data);
        if ($ret['status'] === false) {
            $this->ajaxReturn(0, $ret['msg']);
        }
        $this->ajaxReturn(1, '发布成功', $ret);
    }

    // 获取标签
    public function ajax_get_tags()
    {
        $page = I('get.p', 0, 'intval');
        $limit = 5;
        $count = D('Tag')->count();
        $maxPage = ceil($count / $limit);

        $page = ($page <= $maxPage) ? $page : 1;
        $tags = D('Tag')->page($page, $limit)->getField('tag', true);

        $this->ajaxReturn(1, '', compact('tags', 'page'));
    }

    // 回复
    public function ajax_question_reply()
    {
        $html = $this->fetch();
        $this->ajaxReturn(1, '', $html);
    }

    // 回复保存
    public function ajax_question_reply_save()
    {
        $this->check_login();
        $data['content'] = I('post.content', '', 'trim');
        $uid = session('ytata.uid');
        $qid = I('post.qid', '', 'intval');

        $ret = D('Answer')->answer_add($uid, $qid, $data);

        if ($ret['status'] === false) {
            $this->ajaxReturn(0, $ret['msg']);
        } else {
            $this->ajaxReturn(1, '保存成功');
        }
    }

    // 问题补充
    public function ajax_question_supp()
    {
        $id = I('get.id', '0', 'intval');
        $html = $this->fetch();
        $this->ajaxReturn(1, '', $html);
    }

    // 问题补充保存
    public function ajax_question_supp_save()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $qid = I('request.qid', '0', 'intval');
        $content = I('request.content', '', 'trim,htmlspecialchars');
        if (empty($uid) || empty($qid) || empty($content)) {
            $this->ajaxReturn(0, "参数错误！");
        } else {
            $map['uid'] = $uid;
            $map['qid'] = $qid;
            $map['content'] = $content;
            $map['addtime'] = time();
            M('QuestionSupp')->add($map);
            $this->ajaxReturn(1, "提交成功！");
        }
    }

    // 采纳答案
    public function ajax_adoption_reply()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $rid = I('request.rid', '', 'intval');
        $qid = I('request.qid', '0', 'intval');

        if (empty($uid) || empty($qid) || empty($rid)) {
            $this->ajaxReturn(0, "参数错误");
        }

        $ret = D('Answer')->answer_adoption($uid, $qid, $rid);
        if ($ret['status'] === false) {
            $this->ajaxReturn(0, $ret['msg']);
        } else {
            $this->ajaxReturn(1, '采纳成功');
        }
    }

    // 追问/追答
    public function ajax_question_reply_supp()
    {
        $html = $this->fetch();
        $this->ajaxReturn(1, '', $html);
    }

    // 追问/追答保存
    public function ajax_question_reply_supp_save()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $rid = I('request.rid', '', 'intval');
        $data['type'] = I('request.type', '', 'intval');
        $data['content'] = I('request.content', '', 'trim');
        $ret = D('AnswerReply')->reply_supp($data, $rid, $uid);
        if (!$ret['status']) {
            $this->ajaxReturn(0, $ret['msg']);
        } else {
            $this->ajaxReturn(1, "保存成功");
        }
    }
}